home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / tiff / tools / tiffinfo.c < prev    next >
C/C++ Source or Header  |  1992-02-10  |  8KB  |  317 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/tools/RCS/tiffinfo.c,v 1.11 92/02/10 19:04:25 sam Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1988, 1989, 1990, 1991, 1992 Sam Leffler
  7.  * Copyright (c) 1991, 1992 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. #include <stdio.h>
  30. #include "tiffio.h"
  31.  
  32. typedef    unsigned char u_char;
  33. typedef    unsigned short u_short;
  34. typedef    unsigned short u_int;
  35. typedef    unsigned long u_long;
  36.  
  37. int    showdata = 0;            /* show data */
  38. int    readdata = 0;            /* read data in file */
  39. int    stoponerr = 0;            /* stop on first read error */
  40.  
  41. usage(code)
  42.     int code;
  43. {
  44.     fprintf(stderr, "Usage: tiffinfo [-cdDSjs] [-#] TIFF-files\n");
  45.     fprintf(stderr, "-D    read data\n");
  46.     fprintf(stderr, "-S    stop reading data on 1st read error\n");
  47.     fprintf(stderr, "-d    show data\n");
  48.     fprintf(stderr, "-c    show color/gray response curves and colormap\n");
  49.     fprintf(stderr, "-s    show data strip offsets and byte counts\n");
  50.     fprintf(stderr, "-j    show JPEG tables\n");
  51.     fprintf(stderr, "-#    show directory number #\n");
  52.     exit(code);
  53. }
  54.  
  55. main(argc, argv)
  56.     char *argv[];
  57. {
  58.     int dirnum = -1, multiplefiles, c;
  59.     int verbose = 0;
  60.     int order = 0;
  61.     char *cp;
  62.     TIFF *tif;
  63.     extern int optind;
  64.     long flags = 0;
  65.  
  66.     while ((c = getopt(argc, argv, "cdDSjlmsv0123456789")) != -1)
  67.         switch (c) {
  68.         case '0': case '1': case '2': case '3':
  69.         case '4': case '5': case '6': case '7':
  70.         case '8': case '9':
  71.             dirnum = atoi(argv[optind]);
  72.             break;
  73.         case 'd':
  74.             showdata++;
  75.             /* fall thru... */
  76.         case 'D':
  77.             readdata++;
  78.             break;
  79.         case 'S':
  80.             stoponerr = 1;
  81.             break;
  82.         case 'c':
  83.             flags |= TIFFPRINT_COLORMAP | TIFFPRINT_CURVES;
  84.             break;
  85.         case 'l':
  86.             order = FILLORDER_LSB2MSB;
  87.             break;
  88.         case 'm':
  89.             order = FILLORDER_MSB2LSB;
  90.             break;
  91.         case 's':
  92.             flags |= TIFFPRINT_STRIPS;
  93.             break;
  94.         case 'j':
  95.             flags |= TIFFPRINT_JPEGQTABLES |
  96.                  TIFFPRINT_JPEGACTABLES |
  97.                  TIFFPRINT_JPEGDCTABLES;
  98.             break;
  99.         case 'v':
  100.             verbose = 1;
  101.             break;
  102.         case '?':
  103.             usage(-1);
  104.             /*NOTREACHED*/
  105.         }
  106.     if (optind >= argc)
  107.         usage(-2);
  108.     multiplefiles = (argc - optind > 1);
  109.     for (; optind < argc; optind++) {
  110.         if (multiplefiles || verbose)
  111.             printf("%s:\n", argv[optind]);
  112.         tif = TIFFOpen(argv[optind], "r");
  113.         if (tif != NULL) {
  114.             if (dirnum == -1) {
  115.                 do {
  116.                     if (order)
  117.                         TIFFSetField(tif, TIFFTAG_FILLORDER, order);
  118.                     TIFFPrintDirectory(tif, stdout, flags);
  119.                     if (readdata)
  120.                         TIFFReadData(tif);
  121.                 } while (TIFFReadDirectory(tif));
  122.             } else {
  123.                 if (TIFFSetDirectory(tif, dirnum)) {
  124.                     if (order)
  125.                         TIFFSetField(tif, TIFFTAG_FILLORDER, order);
  126.                     TIFFPrintDirectory(tif, stdout, flags);
  127.                     if (readdata)
  128.                         TIFFReadData(tif);
  129.                 }
  130.             }
  131.             TIFFClose(tif);
  132.         }
  133.     }
  134.     exit(0);
  135. }
  136.  
  137. static
  138. ShowStrip(strip, pp, nrow, scanline)
  139.     int strip;
  140.     register u_char *pp;
  141.     u_long nrow;
  142.     int scanline;
  143. {
  144.     register int cc;
  145.  
  146.     printf("Strip %u:\n", strip);
  147.     while (nrow-- > 0) {
  148.         for (cc = 0; cc < scanline; cc++) {
  149.             printf(" %02x", *pp++);
  150.             if (((cc+1) % 24) == 0)
  151.                 putchar('\n');
  152.         }
  153.         putchar('\n');
  154.     }
  155. }
  156.  
  157. TIFFReadContigStripData(tif)
  158.     TIFF *tif;
  159. {
  160.     u_char *buf;
  161.     int scanline = TIFFScanlineSize(tif);
  162.  
  163.     buf = (u_char *)malloc(TIFFStripSize(tif));
  164.     if (buf) {
  165.         u_long row, h;
  166.         u_long rowsperstrip = (u_long)-1;
  167.  
  168.         TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
  169.         TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
  170.         for (row = 0; row < h; row += rowsperstrip) {
  171.             u_long nrow = (row+rowsperstrip > h ?
  172.                 h-row : rowsperstrip);
  173.             u_int strip = TIFFComputeStrip(tif, row, 0);
  174.             if (TIFFReadEncodedStrip(tif, strip, buf, nrow*scanline) < 0) {
  175.                 if (stoponerr)
  176.                     break;
  177.             } else if (showdata)
  178.                 ShowStrip(strip, buf, nrow, scanline);
  179.         }
  180.         free(buf);
  181.     }
  182. }
  183.  
  184. TIFFReadSeparateStripData(tif)
  185.     TIFF *tif;
  186. {
  187.     u_char *buf;
  188.     int scanline = TIFFScanlineSize(tif);
  189.  
  190.     buf = (u_char *)malloc(TIFFStripSize(tif));
  191.     if (buf) {
  192.         u_long row, h;
  193.         u_long rowsperstrip = (u_long)-1;
  194.         u_short s, samplesperpixel;
  195.  
  196.         TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
  197.         TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
  198.         TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
  199.         for (row = 0; row < h; row += rowsperstrip) {
  200.             for (s = 0; s < samplesperpixel; s++) {
  201.                 u_long nrow = (row+rowsperstrip > h ?
  202.                     h-row : rowsperstrip);
  203.                 u_int strip = TIFFComputeStrip(tif, row, s);
  204.                 if (TIFFReadEncodedStrip(tif, strip, buf, nrow*scanline) < 0) {
  205.                     if (stoponerr)
  206.                         break;
  207.                 } else if (showdata)
  208.                     ShowStrip(strip, buf, nrow, scanline);
  209.             }
  210.         }
  211.         free(buf);
  212.     }
  213. }
  214.  
  215. static
  216. ShowTile(row, col, sample, pp, nrow, rowsize)
  217.     u_long row, col;
  218.     int sample;
  219.     register u_char *pp;
  220.     int nrow, rowsize;
  221. {
  222.     register int cc;
  223.  
  224.     printf("Tile (%lu,%lu", row, col);
  225.     if (sample > 0)
  226.         printf(",%u", sample);
  227.     printf("):\n");
  228.     while (nrow-- > 0) {
  229.         for (cc = 0; cc < rowsize; cc++) {
  230.             printf(" %02x", *pp++);
  231.             if (((cc+1) % 24) == 0)
  232.                 putchar('\n');
  233.         }
  234.         putchar('\n');
  235.     }
  236. }
  237.  
  238. TIFFReadContigTileData(tif)
  239.     TIFF *tif;
  240. {
  241.     u_char *buf;
  242.     int rowsize = TIFFTileRowSize(tif);
  243.  
  244.     buf = (u_char *)malloc(TIFFTileSize(tif));
  245.     if (buf) {
  246.         u_long tw, th, w, h;
  247.         u_long row, col;
  248.  
  249.         TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
  250.         TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
  251.         TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
  252.         TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
  253.         for (row = 0; row < h; row += th) {
  254.             for (col = 0; col < w; col += tw) {
  255.                 if (TIFFReadTile(tif, buf, col, row, 0, 0) < 0) {
  256.                     if (stoponerr)
  257.                         break;
  258.                 } else if (showdata)
  259.                     ShowTile(row, col, -1, buf, th, rowsize);
  260.             }
  261.         }
  262.         free(buf);
  263.     }
  264. }
  265.  
  266. TIFFReadSeparateTileData(tif)
  267.     TIFF *tif;
  268. {
  269.     u_char *buf;
  270.     int rowsize = TIFFTileRowSize(tif);
  271.     u_short samplesperpixel;
  272.  
  273.     buf = (u_char *)malloc(TIFFTileSize(tif));
  274.     if (buf) {
  275.         u_long tw, th, w, h;
  276.         u_long row, col;
  277.         u_short s, samplesperpixel;
  278.  
  279.         TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
  280.         TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
  281.         TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
  282.         TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
  283.         TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
  284.         for (row = 0; row < h; row += th) {
  285.             for (col = 0; col < w; col += tw) {
  286.                 for (s = 0; s < samplesperpixel; s++) {
  287.                     if (TIFFReadTile(tif, buf, col, row, 0, 0) < 0) {
  288.                         if (stoponerr)
  289.                             break;
  290.                     } else if (showdata)
  291.                         ShowTile(row, col, s, buf, th, rowsize);
  292.                 }
  293.             }
  294.         }
  295.         free(buf);
  296.     }
  297. }
  298.  
  299. TIFFReadData(tif)
  300.     TIFF *tif;
  301. {
  302.     u_short config;
  303.  
  304.     TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &config);
  305.     if (TIFFIsTiled(tif)) {
  306.         if (config == PLANARCONFIG_CONTIG)
  307.             TIFFReadContigTileData(tif);
  308.         else
  309.             TIFFReadSeparateTileData(tif);
  310.     } else {
  311.         if (config == PLANARCONFIG_CONTIG)
  312.             TIFFReadContigStripData(tif);
  313.         else
  314.             TIFFReadSeparateStripData(tif);
  315.     }
  316. }
  317.